layui.extend({
    admin: '/static/background/js/admin'
});
layui.use(['table', 'jquery', 'form', 'admin'], function () {
    let form = layui.form,
        $ = layui.jquery;
    if (localStorage.getItem('Authorization')) {
        let text = '<a href="/person?id=' + localStorage.getItem('id') + '" style="color: white">' + localStorage.getItem('name') + '</a>'
        $('.navbar-right li').eq(1).html(text)
        $('#logout').show()
        $('#photo').attr('src', localStorage.getItem('photo'))
        $('#photo').attr('onclick', 'location.href="/person?id=' + localStorage.getItem('id') + '"')
        $('.avatar').attr('src', localStorage.getItem('photo'))
        $('.author').html(localStorage.getItem('name'))
    }

    $(document).on('click', '#logout', function () {
        localStorage.clear()
        location.href = '/'
    })

    form.on('submit(login)', function (data) {
        let username = $("input[name=username]").val();
        let password = $("input[name=password]").val();
        console.log($(this).val())
        if ($(this).val() === "登录") {
            $.ajax({
                type: "POST",
                url: '/api/user/auth',
                dataType: 'json',
                data: {
                    username: username,
                    password: password,
                },
                success: function (data) {
                    if (data.status === 200) {
                        localStorage.id = data.data.id
                        localStorage.name = data.data.name
                        localStorage.Authorization = data.data.Authorization
                        localStorage.photo = data.data.photo
                        location.href = '/'
                    } else {
                        layui.use('layer', function () {
                            layer.msg(data.msg)
                        });
                    }
                },
                err: function () {
                    layui.use('layer', function () {
                        layer.msg("服务器请求失败")
                    });
                }
            });
        } else {
            if (username.length < 5) {
                layer.msg('用户名过短')
                return
            }
            if (password.length < 6) {
                layer.msg('密码过短')
                return
            }
            $.ajax({
                type: "POST",
                url: '/api/user/addUser',
                dataType: 'json',
                data: {
                    username: username,
                    password: password,
                },
                success: function (data) {
                    if (data.status === 200) {
                        layui.use('layer', function () {
                            layer.msg('创建成功')
                        })
                        setTimeout(function () {
                            window.location.reload()
                        }, 1000);
                    } else {
                        layui.use('layer', function () {
                            layer.msg(data.msg)
                        });
                    }
                },
                err: function () {
                    layui.use('layer', function () {
                        layer.msg("服务器请求失败")
                    });
                }
            });
        }
        return false;
    });
});